//
//  CollectViewController.m
//  艾听歌
//
//  Created by dllo on 16/6/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CollectViewController.h"
#import "SongListModel.h"

@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) NSMutableArray *array;


@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    
    
    [self createImgView];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.image addSubview:self.tableView];
    [self.view bringSubviewToFront:self.tableView];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.image.userInteractionEnabled = YES;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    [self dataHandle];
    [self.tableView reloadData];
}
-(void)dataHandle
{
 self.array = [[WTDataBaseHandle shareDataFMDBHandle] selectMusicPlaywithCompare:YES];
    for (SongListModel *model in self.array) {
        NSLog(@"%@",model.name);
    }
}
- (void)createImgView
{
    self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"content-empty"]];
    self.image.contentMode = UIViewContentModeScaleAspectFit;
    self.image.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    
    [self.view addSubview:self.image];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count == 0) {
    return 1;
}else{
    
    return self.array.count;
}
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (self.array.count == 0) {
        cell.textLabel.text = @"暂无收藏,请先去收藏";
        return cell;
    }else{
        SongListModel *model = self.array[indexPath.row];
        cell.textLabel.text = model.name;
        return cell;
    }
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath


{
    
    
    SongListModel *songModel = self.array[indexPath.row];
    NSLog(@"%@",songModel.songId);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if (self.array.count >= 1) {
            [[WTDataBaseHandle shareDataFMDBHandle] deleteMusicId:songModel.songId];
            [self.array removeObjectAtIndex:indexPath.row];
            [tableView reloadData];

        }
        
        
    }
    
    }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
